package com.zry.traced.presenter

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import com.zry.traced.MainActivity
import com.zry.traced.R
import com.zry.traced.R.id.submitCodeRecycler
import com.zry.traced.adapter.CodeListStringAdapter
import com.zry.traced.adapter.TraceCodeListAdapter
import com.zry.traced.animation.RecyclerViewUtilKt
import com.zry.traced.been.PackResultCode
import com.zry.traced.been.ResultCodeData
import com.zry.traced.greendao.gen.PackingOperationInfoDao.Properties.PackProportion
import com.zry.traced.greendao.gen.PackingOperationInfoDao.Properties.PackUnitName
import com.zry.traced.retrofit.ShowUserView
import com.zry.traced.ui.activity.OperationRecordInfoActivity
import com.zry.traced.ui.activity.SubmitPutStorageActivity
import com.zry.traced.utils.ACacheUtil
import com.zry.traced.utils.DateTimeUtilsKt
import com.zry.traced.utils.StringListUtil
import com.zry.traced.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_submit_put_storage.*


class SubmitPutStoragePresenter(private val activity: SubmitPutStorageActivity) : ShowUserView {

    @SuppressLint("SetTextI18n")
    fun initView() {
        when (activity.operationType) {
            "批量装箱", "号段装箱" -> {
                activity.submitProductName.text = activity.packingOperationInfo!!.productName
                activity.submitProductSpec.text = activity.packingOperationInfo!!.netContent + activity.packingOperationInfo!!.unitName + "/" + activity.packingOperationInfo!!.packName
                setCodeView(activity.packingOperationInfo!!.submitCode, activity.packingOperationInfo!!.submitMinCode, activity.packingOperationInfo!!.submitMaxCode)
            }
            "批量入库", "号段入库" -> {
                activity.submitProductName.text = activity.putStorageCode!!.productName
                activity.submitProductSpec.text = activity.putStorageCode!!.netUnitPackName
                setCodeView(activity.putStorageCode!!.codeS, activity.putStorageCode!!.minCode, activity.putStorageCode!!.maxCode)
            }
            "批量出库", "号段出库" -> {
                activity.submitProductName.text = activity.outTreasuryCode!!.productName
                activity.submitProductSpec.text = activity.outTreasuryCode!!.netContent + activity.outTreasuryCode!!.unitName + "/" + activity.outTreasuryCode!!.productSpecName
                activity.submitOutgoingWayLinear.visibility = View.VISIBLE
                activity.submitOutgoingWayTV.text = activity.outTreasuryCode!!.outLevelName
                setCodeView(activity.outTreasuryCode!!.codes, activity.outTreasuryCode!!.minCode, activity.outTreasuryCode!!.maxCode)
            }
        }
    }


    private fun setCodeView(codes: String, minCode: String, maxCode: String) {
        if (codes != "" && codes != null) {
            val codeStringList: MutableList<String> = StringListUtil().getList(codes)
            activity.submitSectionNumberTV.text = codeStringList.size.toString()
            initRecycler(codeStringList)
            activity.submitCodeRecycler.visibility = View.VISIBLE
            activity.submitCodeNumberLinear.visibility = View.GONE
        } else {
            activity.submitCodeRecycler.visibility = View.GONE
            activity.submitCodeNumberLinear.visibility = View.VISIBLE
            activity.submitStartCode.text = minCode
            activity.submitEndCode.text = maxCode
            activity.submitSectionNumberTV.text = ((StringListUtil().getStringIntercept(maxCode, 8).toInt()) - (StringListUtil().getStringIntercept(minCode, 8).toInt()) + 1).toString()
        }
    }

    private fun initRecycler(codeStringList: MutableList<String>) {
        val recyclerViewUtilKt = RecyclerViewUtilKt(activity, activity.submitCodeRecycler)
        recyclerViewUtilKt.initRecyclerView()

        val codeListStringAdapter = CodeListStringAdapter(R.layout.code_list_item, codeStringList)
        recyclerViewUtilKt.setAdapter(codeListStringAdapter)
    }


    fun submitPutStorage(view: View) {
        when (view.id) {
            R.id.submitPutStorageBtn -> {
                putData()
            }
        }
    }


    /**
     *提交数据
     */
    private fun putData() {
        when (activity.title) {
            "批量装箱" -> {
                HttpServer(this).postAddLiangFlow(
                        activity.packingOperationInfo!!.packLevelID,
                        activity.packingOperationInfo!!.productID,
                        activity.packingOperationInfo!!.packUnitID,
                        activity.packingOperationInfo!!.packProportion,
                        activity.packingOperationInfo!!.submitCode,
                        ACacheUtil().getLoginInfo().userid,
                        ACacheUtil().getLoginInfo().EntID,
                        activity)
            }
            "号段装箱" -> {
                HttpServer(this).addDuanFlow(
                        activity.packingOperationInfo!!.packLevelID,
                        activity.packingOperationInfo!!.productID,
                        activity.packingOperationInfo!!.packUnitID,
                        activity.packingOperationInfo!!.packProportion,
                        activity.packingOperationInfo!!.submitMaxCode,
                        activity.packingOperationInfo!!.submitMinCode,
                        ACacheUtil().getLoginInfo().userid,
                        ACacheUtil().getLoginInfo().EntID,
                        activity)
            }
            "批量入库" -> {
                HttpServer(this).addLiangStore(
                        activity.putStorageCode!!.orderID,
                        activity.putStorageCode!!.codeS,
                        ACacheUtil().getLoginInfo().userid,
                        activity)
            }
            "号段入库" -> {
                HttpServer(this).addDuanStore(
                        activity.putStorageCode!!.orderID,
                        activity.putStorageCode!!.maxCode,
                        activity.putStorageCode!!.minCode,
                        ACacheUtil().getLoginInfo().userid,
                        activity)
            }
            "批量出库" -> {
                HttpServer(this).addLiangOutStockDetail(
                        activity.outTreasuryCode!!.outStockID,
                        activity.outTreasuryCode!!.productID,
                        activity.outTreasuryCode!!.outLevelID,
                        activity.outTreasuryCode!!.codes,
                        ACacheUtil().getLoginInfo().userid,
                        ACacheUtil().getLoginInfo().EntID,
                        activity
                )
            }
            "号段出库" -> {
                HttpServer(this).addDuanOutStockDetail(
                        activity.outTreasuryCode!!.outStockID,
                        activity.outTreasuryCode!!.productID,
                        activity.outTreasuryCode!!.outLevelID,
                        activity.outTreasuryCode!!.minCode,
                        activity.outTreasuryCode!!.maxCode,
                        ACacheUtil().getLoginInfo().userid,
                        ACacheUtil().getLoginInfo().EntID,
                        activity
                )
            }
        }
    }

    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            7 -> {
                val resultData: ResultCodeData<MutableList<PackResultCode>> = data as ResultCodeData<MutableList<PackResultCode>>
                if (resultData.state == "1") {
                    //删除数据库数据
                    activity.packingOperationInfoDao?.deleteByKey(activity.timeID)

                    ToastUtil.toast(activity, "提交成功")
                    val intent = Intent(activity, OperationRecordInfoActivity::class.java)
                    intent.putExtra("title", activity.title)
                    intent.putExtra("FlowCodeID", resultData.FlowCodeID)
                    activity.startActivity(intent)
                } else {
                    return ToastUtil.toast(activity, resultData.Message)
                }
            }
            10 -> {
                val resultData: ResultCodeData<MutableList<PackResultCode>> = data as ResultCodeData<MutableList<PackResultCode>>
                if (resultData.state == "1") {
                    //删除数据库数据
                    activity.packingOperationInfoDao?.deleteByKey(activity.timeID)

                    ToastUtil.toast(activity, "提交成功")
                    val intent = Intent(activity, OperationRecordInfoActivity::class.java)
                    intent.putExtra("title", activity.title)
                    intent.putExtra("FlowCodeID", resultData.FlowCodeID)
                    activity.startActivity(intent)
                } else {
                    return ToastUtil.toast(activity, resultData.Message)
                }
            }
            12 -> {
                val resultData: ResultCodeData<MutableList<PackResultCode>> = data as ResultCodeData<MutableList<PackResultCode>>
                if (resultData.state == "1") {
                    //删除数据库数据
                    activity.putStorageCodeDao?.deleteByKey(activity.timeID)
                    goToMainPage()
                } else {
                    return ToastUtil.toast(activity, resultData.Message)
                }
            }
            13 -> {
                val resultData: ResultCodeData<MutableList<PackResultCode>> = data as ResultCodeData<MutableList<PackResultCode>>
                if (resultData.state == "1") {
                    //删除数据库数据
                    activity.putStorageCodeDao?.deleteByKey(activity.timeID)
                    goToMainPage()
                } else {
                    return ToastUtil.toast(activity, resultData.Message)
                }
            }
            17 -> {
                val resultData: ResultCodeData<MutableList<PackResultCode>> = data as ResultCodeData<MutableList<PackResultCode>>
                if (resultData.state == "1") {
                    //删除数据库数据
                    activity.outTreasuryCodeDao?.deleteByKey(activity.timeID)
                    goToMainPage()
                } else {
                    return ToastUtil.toast(activity, resultData.Message)
                }
            }
            18 -> {
                val resultData: ResultCodeData<MutableList<PackResultCode>> = data as ResultCodeData<MutableList<PackResultCode>>
                if (resultData.state == "1") {
                    //删除数据库数据
                    activity.outTreasuryCodeDao?.deleteByKey(activity.timeID)
                    goToMainPage()
                } else {
                    return ToastUtil.toast(activity, resultData.Message)
                }
            }
        }
    }

    private fun goToMainPage() {
        ToastUtil.toast(activity, "提交成功")
        val intent = Intent(activity, MainActivity::class.java)
        activity.startActivity(intent)
    }
}